<!DOCTYPE html>
<html lang="zh">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>云盘上传</title>
  </head>

  <body>
    <input id="file" type="file" accept="audio/mpeg" />

    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.20.0-0/axios.min.js"></script>
    <script>

      const phone = ''
      const password = ''
      let cookieToken = ''
      if (!phone || !password) {
        const msg = '请设置你的手机号码和密码'
        alert(msg)
        throw new Error(msg)
      }
      login()
      main()
      async function login() {
        const res = await axios({
          url: `/login/cellphone?phone=${phone}&password=${password}`,
          withCredentials: true, //关键
        })
        cookieToken = res.data.cookie
      }
      async function main() {
        document
          .querySelector('input[type="file"]')
          .addEventListener('change', function (e) {
            var file = this.files[0]
            upload(file)
          })
      }

      async function upload(file) {
        var formData = new FormData()
        formData.append('songFile', file)
        const res = await axios({
          method: 'post',
          url: `http://localhost:3000/cloud?time=${Date.now()}`,
          headers: {
            'Content-Type': 'multipart/form-data',
          },
          data: formData,
        })
      }
    </script>
  </body>
</html>
